/*
    SUSAN® - Sole of Unix Save ANything

   Copyright (C) 2018-2020 Hechzh Property

   
*/

#include "include/susan.h"
#include "lib/tls_conf.h"

bool TlsResource::IsTlsConfigured() const
{
  return tls_enable_ || tls_require_;
}

TlsPolicy TlsResource::GetPolicy() const
{
  TlsPolicy result = TlsPolicy::kBnetTlsNone;
  if (tls_enable_) { result = TlsPolicy::kBnetTlsEnabled; }
  if (tls_require_) { result = TlsPolicy::kBnetTlsRequired; }
  return result;
}

int TlsResource::SelectTlsPolicy(TlsPolicy remote_policy) const
{
  if (remote_policy == TlsPolicy::kBnetTlsAuto) {
    return TlsPolicy::kBnetTlsAuto;
  }
  TlsPolicy local_policy = GetPolicy();

  if ((remote_policy == 0 && local_policy == 0) ||
      (remote_policy == 0 && local_policy == 1) ||
      (remote_policy == 1 && local_policy == 0)) {
    return TlsPolicy::kBnetTlsNone;
  }
  if ((remote_policy == 0 && local_policy == 2) ||
      (remote_policy == 2 && local_policy == 0)) {
    return TlsPolicy::kBnetTlsDeny;
  }
  return TlsPolicy::kBnetTlsEnabled;
}
